package com.hankai.controller.portal;

import com.hankai.common.Const;
import com.hankai.common.ResponseCode;
import com.hankai.common.ServerResponse;
import com.hankai.pojo.User;
import com.hankai.service.ICartService;
import com.hankai.vo.CartVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by hankai on 2017/6/8.
 */
@Controller
@RequestMapping("/cart/")
public class CartController {
    @Resource
    private ICartService iCartService;

    /**
     * 添加购物车商品
     * @param session
     * @param count
     * @param productId
     * @return
     */
    @RequestMapping(value = "addCart.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> addCart(HttpSession session,Integer count,Integer productId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.addCart(user.getId(),productId,count);
    }

    /**
     * 修改购物车商品
     * @param session
     * @param count
     * @param productId
     * @return
     */
    @RequestMapping(value = "updateCart.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> updateCart(HttpSession session,Integer count,Integer productId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.updateCart(user.getId(),productId,count);
    }

    /**
     * 删除购物车商品（可能会删除多个，通过字符串拼接id）
     * @param session
     * @param count
     * @param productIds
     * @return
     */
    @RequestMapping(value = "deleteCart.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> deleteCart(HttpSession session, Integer count, String productIds){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.deleteCart(user.getId(),productIds);
    }

    /**
     * 获取购物车列表
     * @param session
     * @return
     */
    @RequestMapping(value = "getCartList.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> getCartList(HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.getCartList(user.getId());
    }

    /**
     * 全选中
     * @param session
     * @return
     */
    @RequestMapping(value = "selectAll.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> selectAll(HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.updateSelectOrUnSelect(user.getId(),null,Const.Cart.CHECKED);
    }

    /**
     * 全反选
     * @param session
     * @return
     */
    @RequestMapping(value = "unSelectAll.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> unSelectAll(HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.updateSelectOrUnSelect(user.getId(),null,Const.Cart.UNCHECKED);
    }

    /**
     * 单选
     * @param session
     * @param productId
     * @return
     */
    @RequestMapping(value = "select.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> select(HttpSession session,Integer productId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.updateSelectOrUnSelect(user.getId(),productId,Const.Cart.CHECKED);
    }

    /**
     * 反选
     * @param session
     * @param productId
     * @return
     */
    @RequestMapping(value = "unSelect.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> unSelect(HttpSession session,Integer productId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.updateSelectOrUnSelect(user.getId(),productId,Const.Cart.UNCHECKED);
    }

    /**
     * 获取所有数量
     * @param session
     * @return
     */
    @RequestMapping(value = "getCount.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Integer> getCount(HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createBySuccess(0);
        }
        return iCartService.getCount(user.getId());
    }
}
